//
//  CommunityNavigationTitleView.m
//  ArtDog
//
//  Created by dllo on 16/9/2.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CommunityNavigationTitleView.h"
#import "Masonry.h"
#import "UIColor+Common.h"
#import "HLMacro.h"

#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height

@interface CommunityNavigationTitleView ()

@property (nonatomic, strong) UIButton *recommendBtn;

@property (nonatomic, strong) UIButton *viewerBtn;

@property (nonatomic, strong) UIButton *bestNewBtn;

@property (nonatomic, strong) UIView *lineView;

@end

@implementation CommunityNavigationTitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
        [self layoutAllViews];
        
    }
    return self;
}

- (void)setupViews
{
    _recommendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_recommendBtn];
    _recommendBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_recommendBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_recommendBtn setTitle:@"推荐" forState:UIControlStateNormal];
    [_recommendBtn addTarget:self action:@selector(recommendAction) forControlEvents:UIControlEventTouchUpInside];
    
    _bestNewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_bestNewBtn];
    _bestNewBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_bestNewBtn setTitleColor:[UIColor colorFromHexRGB:@"8a8a8a"] forState:UIControlStateNormal];
    [_bestNewBtn setTitle:@"最新" forState:UIControlStateNormal];
    [_bestNewBtn addTarget:self action:@selector(bestNewAction) forControlEvents:UIControlEventTouchUpInside];
    
    _viewerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_viewerBtn];
    _viewerBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_viewerBtn setTitleColor:[UIColor colorFromHexRGB:@"8a8a8a"] forState:UIControlStateNormal];
    [_viewerBtn setTitle:@"关注" forState:UIControlStateNormal];
    [_viewerBtn addTarget:self action:@selector(viewerAction) forControlEvents:UIControlEventTouchUpInside];
    
    _lineView = [[UIView alloc]initWithFrame:CGRectMake(WIDTH / 6 - 17, HEIGHT - 1, 34, 1)];
    [self addSubview:_lineView];
    _lineView.backgroundColor = [UIColor blackColor];
}

- (void)layoutAllViews
{
    [_recommendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.centerX.equalTo(self).multipliedBy(0.333333);
    }];
    [_bestNewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.centerX.equalTo(self);
    }];
    [_viewerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.centerX.equalTo(self).multipliedBy(1.666667);
    }];
}

#pragma mark - button clicked
- (void)recommendAction
{
    if (_lineView.frame.origin.x != WIDTH / 6 - 17) {
        [_recommendBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_bestNewBtn setTitleColor:[UIColor colorFromHexRGB:@"8a8a8a"] forState:UIControlStateNormal];
        [_viewerBtn setTitleColor:[UIColor colorFromHexRGB:@"8a8a8a"] forState:UIControlStateNormal];
        
        [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            _lineView.frame = CGRectMake(WIDTH / 6 - 17, HEIGHT - 1, 34, 1);
        } completion:nil];
        if (_collectionViewOffset) {
            _collectionViewOffset(0);
        }
    }
}

- (void)bestNewAction
{
    if (_lineView.frame.origin.x != WIDTH / 2 - 17) {
        [_bestNewBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_recommendBtn setTitleColor:[UIColor colorFromHexRGB:@"8a8a8a"] forState:UIControlStateNormal];
        [_viewerBtn setTitleColor:[UIColor colorFromHexRGB:@"8a8a8a"] forState:UIControlStateNormal];
        
        [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            _lineView.frame = CGRectMake(WIDTH / 2 - 17, HEIGHT - 1, 34, 1);
        } completion:nil];
        if (_collectionViewOffset) {
            _collectionViewOffset(SCREEN_WIDTH);
        }
    }
}

- (void)viewerAction
{
    if (_lineView.frame.origin.x != WIDTH * 5 / 6 - 17) {
        [_viewerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_bestNewBtn setTitleColor:[UIColor colorFromHexRGB:@"8a8a8a"] forState:UIControlStateNormal];
        [_recommendBtn setTitleColor:[UIColor colorFromHexRGB:@"8a8a8a"] forState:UIControlStateNormal];
        
        [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            _lineView.frame = CGRectMake(WIDTH * 5 / 6 - 17, HEIGHT - 1, 34, 1);
        } completion:nil];
        if (_collectionViewOffset) {
            _collectionViewOffset(2 * SCREEN_WIDTH);
        }
    }
    
}

@end
